import Vue from 'vue'
import VueRouter from 'vue-router'

// import { getToken } from '@/utils/storage'
import store from '@/store/index'

import routes from './routes'

Vue.use(VueRouter)

const router = new VueRouter({
  routes
})

// 路由鉴权
// 通过路由前置守卫判断是否有token

const whiteList = ['/login']

router.beforeEach((to, from, next) => {
  // token
  // const token = getToken()
  const { token } = store.state.user

  if (token) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
